package cn.ljx.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ljx.entity.JsonReturn;
import cn.ljx.entity.TBProblemType;
import cn.ljx.service.TBProblemTypeService;
import cn.ljx.util.BaseUtils;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/Problem")
public class ProblemController {
	@Autowired
    private TBProblemTypeService tbProblemTypeService;
	
	@SuppressWarnings("finally")
	@ResponseBody
	@RequestMapping(value="/getProblemList",method=RequestMethod.POST)
    public String gettbProblemList(String problem){
		JsonReturn jr = new JsonReturn();
		try{
			//Map<>result
			List<TBProblemType> tbproblemtypeList=tbProblemTypeService.gettbProblemList(problem);
	        if(tbproblemtypeList!=null){
	        	jr.setObject(tbproblemtypeList);
	        	jr.setInfo("success");
	        }
		}catch(Exception e){
			jr.setInfo("false");
		}finally{
			return JSON.toJSONString(jr);
		}
    }
	
	@ResponseBody
	@RequestMapping(value="/delectProblem",method=RequestMethod.POST)
	public JsonReturn delectProblem(String id){
		JsonReturn jr = new JsonReturn();
		int b = tbProblemTypeService.delectProblem(id);
		if(b!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addProblem",method=RequestMethod.POST)
	public JsonReturn addProblem(TBProblemType tbproblemtype){
		JsonReturn jr = new JsonReturn();
		try {
			tbproblemtype.setId(BaseUtils.getUUID32());
			tbproblemtype.setCreate_time(BaseUtils.getTime());
			tbproblemtype.setState("1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = tbProblemTypeService.addProblem(tbproblemtype);
		if(i==1){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//获得某一故障问题的数据
	@ResponseBody
	@RequestMapping(value="/getProblemById",method=RequestMethod.POST)
	public JsonReturn getProblemById(String id){
		JsonReturn jr = new JsonReturn();
		TBProblemType tbProblemType = tbProblemTypeService.getProblemById(id);
		if(tbProblemType!=null){
			jr.setInfo("success");
			jr.setObject(tbProblemType);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	
		
		@ResponseBody
		@RequestMapping(value="/updateProblem",method=RequestMethod.POST)
		public JsonReturn updateUser(TBProblemType tbproblemtype){
			JsonReturn jr = new JsonReturn();
			int i = tbProblemTypeService.updateProblem(tbproblemtype);
			if(i!=0){
				jr.setInfo("success");
			}else{
				jr.setInfo("flase");
			}
			return jr;
		}
}